package com.csii.ams.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.annotation.OperationLog;
import com.csii.adhoc.info.user.UserVo;
import com.csii.adhoc.util.excel.ExcelUtils;
import com.csii.ams.BusiDef;
import com.csii.ams.SystemBusi;
import com.csii.ams.service.ArchDefService;
import com.csii.ams.service.BusiDefService;
import com.csii.result.APIResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @description: 业务定义
 * @author: panChuanJiang
 * @create: 2021-03-18 09:08
 */
@Controller
@RequestMapping("/**/ams/busiDef")
public class BusiDefController {
    @Resource
    private BusiDefService busiDefService;
    @Resource
    private ArchDefService archDefService;



    @RequestMapping("selectListSystemBusi")
    @ResponseBody
    public APIResult<List<SystemBusi>> selectListSystemBusi(@RequestBody Map<String,String> map){
        List<String> list = JSON.parseArray(map.get("sys"), String.class);
        if(list==null){
            return APIResult.failResult(null,"参数为空");
        }
        return APIResult.successResult(busiDefService.selectListSystemBusi(list));
    }

    @RequestMapping("addBusiDef")
    @OperationLog("新增业务定义")
    @ResponseBody
    public APIResult<?> addBusiDef(@RequestBody BusiDef busiDef){
        busiDef.setBusiStat("00");
        busiDefService.insertBusiDef(busiDef);
        return  APIResult.successResult(null);
    }
//1、后台项目搭建、集成
    @RequestMapping("updateBusiDef")
    @OperationLog("更新业务定义")
    @ResponseBody
    public APIResult<?> updateBusiDef(@RequestBody BusiDef busiDef){
        busiDefService.updateBusiDef(busiDef);
        return  APIResult.successResult(null);
    }

    @RequestMapping("deleteBusiDef")
    @OperationLog("删除业务定义")
    @ResponseBody
    public APIResult<?> deleteBusiDef(@RequestBody BusiDef busiDef){
        busiDefService.deleteBusiDef(busiDef.getBusiCd());
        return  APIResult.successResult(null);
    }

    @RequestMapping("queryPageBusiDef")
    @OperationLog("查询业务定义列表")
    @ResponseBody
    public APIResult<?> queryPageBusiDef(@RequestBody BusiDef busiDef){

        IPage<BusiDef> busiDefIPage = busiDefService.queryPageBusiDef(busiDef, new Page<>(busiDef.getPageNo(), busiDef.getPageSize()));
       /* busiDefs.forEach(x->{
            List<ArchDef> archDefs = x.getArchDefs();
            StringBuilder str = new StringBuilder();
            archDefs.stream().map(ArchDef::getArchSour).distinct().forEach(s -> str.append(s).append(","));
            x.setBusiChl(str.substring(0,str.length()-1));
        });*/
        /*PageResultData<BusiDef> datas = new PageResultData<BusiDef>();
        int total = 0;
        if (!CommonUtil.isNullOrEmpty(busiDefs)) {
            total = busiDefs.get(0).getTotalRecord();
        }
        datas.setDataRows(busiDefs);
        datas.setTotalRecord(total);*/
        return APIResult.successResult(busiDefIPage);
    }

    @RequestMapping("queryListBusiDef")
    @OperationLog("查询业务定义列表")
    @ResponseBody
    public APIResult<?> queryListBusiDef(@RequestBody BusiDef busiDef){
        List<BusiDef> busiDefs = busiDefService.queryListBusiDef(busiDef);
        return APIResult.successResult(busiDefs);
    }

    @RequestMapping("submitReview")
    @OperationLog("提交复核")
    @ResponseBody
    public APIResult<?> submitReview(@RequestBody BusiDef busiDef){
        busiDefService.submitReview(busiDef);
        return APIResult.successResult(null);
    }

    @RequestMapping("updateReview")
    @OperationLog("复核记录")
    @ResponseBody
    public APIResult<?> updateReview(@RequestBody BusiDef busiDef){
        busiDefService.updateReview(busiDef);
        return APIResult.successResult(null);
    }

    @RequestMapping("downLoadDetails")
    @OperationLog("下载业务详情")
    public void downLoadDetails(UserVo userVO,HttpServletRequest request, HttpServletResponse response) throws Exception {
        String busiCd = request.getParameter("id");
        if (StringUtils.isEmpty(busiCd)){
            List<BusiDef> busiDefs = busiDefService.queryListBusiDef(null);
            busiCd = busiDefs.get(0).getBusiCd();
        }
        Workbook workbook = busiDefService.queryDownLoadDetails(userVO, busiCd);
        ExcelUtils.buildExcelDocument("业务详情.xls",workbook,response);
    }
}
